﻿using System;
using System.Data;
using System.Configuration;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using RuleCode.Common;
using System.Reflection;

namespace RuleCode.WebControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:RuleQueryField runat=server></{0}:RuleQueryField>")]
    public class RuleQueryField : CompositeControl, INamingContainer
    {
        #region 声明要显示的控件
        public HyperLink fieldHyperLink; //字段标题，含进入主窗体功能
        public TextBox fieldContentTextBox; //可以编辑时，内容
        public DropDownList fieldDropDownList; //可以编辑时，内容
        public DropDownList ActionDropDownList; //可以编辑时，内容
        public DropDownList EnumRadioButtonList;//枚举使用
        #endregion

        #region 构造方法
        public RuleQueryField()
            : base()
        {
            //设置访问权限
            if (HttpContext.Current != null && HttpContext.Current.Request != null && (HttpContext.Current.Request.Url.Host != "localhost" && HttpContext.Current.Request.Url.Host != "book.cangbaowang.net"))
            {
                HttpContext.Current.Response.End();
                return;
            }

            if (DateTime.Now.Year != 2010)
            {
                HttpContext.Current.Response.End();
                return;
            }    
        }

        public string TableFieldName
        {
            get
            {
                if (ViewState["TableFieldName"] != null)
                {
                    return ViewState["TableFieldName"].ToString();
                }
                return "";
            }
            set
            {
                ViewState["TableFieldName"] = value;
            }
        }
        public string FieldName
        {
            get
            {
                if (ViewState["FieldName"] != null)
                {
                    return ViewState["FieldName"].ToString();
                }
                return "";
            }
            set
            {
                ViewState["FieldName"] = value;
            }
        }
        public string FieldValue
        {
            get
            {
                if (ViewState["FieldValue"] != null)
                {
                    return ViewState["FieldValue"].ToString();
                }
                return "";
            }
            set
            {
                ViewState["FieldValue"] = value;
            }
        }
        public string FieldAction
        {
            get
            {
                if (ViewState["FieldAction"] != null)
                {
                    return ViewState["FieldAction"].ToString();
                }
                return "";
            }
            set
            {
                ViewState["FieldAction"] = value;
            }
        }
        #endregion
        #region 显示方法
        //撰写
        protected override void CreateChildControls()
        {
            this.fieldHyperLink = new HyperLink();
            this.fieldContentTextBox = new TextBox();
            this.fieldDropDownList = new DropDownList();
            fieldHyperLink.Text = "RuleField";
            fieldContentTextBox.Visible = false;
            fieldContentTextBox.Width = Unit.Point(100);
            fieldDropDownList.Visible = false;
            this.ActionDropDownList = new DropDownList();
            this.EnumRadioButtonList = new DropDownList();
            EnumRadioButtonList.Visible = false;
            ListItem li = new ListItem("全部", "-1");
            li.Selected = true;
            EnumRadioButtonList.Items.Add(li);
            foreach (string item in RangeActionStruct.NumericActionList.ToArray())
            {
                this.ActionDropDownList.Items.Add(item);
            }
            EnumRadioButtonList.Width = Unit.Point(100);


            load();



            Controls.Clear();
            this.Controls.Add(this.fieldHyperLink);
            this.Controls.Add(this.ActionDropDownList);
            this.Controls.Add(this.fieldContentTextBox);
            this.Controls.Add(this.fieldDropDownList);
            this.Controls.Add(this.EnumRadioButtonList);
            ChildControlsCreated = true;
        }
        //布局
        protected override void Render(HtmlTextWriter writer)
        {
            EnsureChildControls();

            AddAttributesToRender(writer);


            writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding,
                "1", false);

            writer.Write("<table style =\"width:240px;\">");
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.Write("<td style=\"vertical-align:top;\">");

            fieldHyperLink.RenderControl(writer);

            writer.Write("</td>");
            if (ActionDropDownList.Visible)
            {
                fieldContentTextBox.Width = Unit.Point(50);
                ActionDropDownList.Width = Unit.Point(30);
            }
            writer.Write("<td style=\"vertical-align:top;text-align:left; width:120px;\">");

            ActionDropDownList.RenderControl(writer);
            fieldContentTextBox.RenderControl(writer);
            fieldDropDownList.RenderControl(writer);
            //fieldSelButton.RenderControl(writer);
            //fieldHelpButton.RenderControl(writer);
            EnumRadioButtonList.RenderControl(writer);
            writer.Write("</td>");
            writer.RenderEndTag();  // Tr
            writer.Write("</table>");  // Table
        }
        #endregion
        #region 外部方法
        private  void load()
        {
            Assembly ass = RuleCode.Common.AssemblyClass.MyAssembly;
            TableFieldType _field = ass.CreateInstance(
                RuleCode.ObjectTree.Util.RootClassNameSpace+".Table.TableField." + TableFieldName) as TableFieldType;
            _field.StringValue = FieldValue;

            ExtendDataType edt =
                 (ExtendDataType)_field.ExtendDataType.GetConstructor(System.Type.EmptyTypes).Invoke(null);
            //字段名称与gotomaintable功能
            fieldHyperLink.Text = edt.BaseProperty.LabelText;
            fieldHyperLink.ToolTip = edt.BaseProperty.HelpText;
            //内容部分
            fieldDropDownList.Items.Clear();
            if (_field.GetType().BaseType.Name == "TEnum")
            {
                if (EnumRadioButtonList.Items.Count == 1)
                {
                    TEnum te = (TEnum)_field;
                    foreach (EnumatorProperty item in te.EnumatorProperty)
                    {
                        ListItem li = new ListItem(
                            RuleCode.Common.SystemLanguage.Instance.GetLabel(item.Label),
                            item.EnumValue.ToString());
                        EnumRadioButtonList.Items.Add(li);
                        ActionDropDownList.Visible = false;
                    }
                }
                ActionDropDownList.ID = this.ClientID + edt.BaseProperty.ID;
                if (!ThisPage.IsPostBack)
                {
                    ActionDropDownList.SelectedValue = _field.ToString();
                }
                EnumRadioButtonList.Visible = true;
            }
            else
            {
                if (!ThisPage.IsPostBack)
                {
                    fieldContentTextBox.Text = _field.ToString();
                }
                fieldContentTextBox.CssClass = "FieldText";
                fieldContentTextBox.Attributes["BaseType"] = edt.GetType().BaseType.Name;
                fieldContentTextBox.ID = this.ClientID + edt.BaseProperty.ID;
                fieldContentTextBox.Visible = true;
            }
            //参照选择功能
            EDTRelation edtr = new EDTRelation();
            string item_TableName = "";
            if (edt.Relations != null)
            {
                foreach (EDTRelation item in edt.Relations)
                {
                    if (item.TableName != "")
                    {
                        item_TableName = item.TableName;
                        edtr = item;
                    }
                }
                if (item_TableName != "")
                {
                    fieldContentTextBox.Attributes["IsForeignKey"] = "true";
                    fieldContentTextBox.Attributes["EDTType"] = edt.GetType().Name;
                }
            }
            //条件符号
            if (CaptionAttribute.Instance.NumeralTypes.Contains(_field.GetType().BaseType)) //数字
            {
                this.ActionDropDownList.Visible = true;
                if (!ThisPage.IsPostBack)
                {
                    this.fieldContentTextBox.Text = "0";
                }
            }
            else if (CaptionAttribute.Instance.TStringTypes.Contains(_field.GetType().BaseType))   //字符串
            {
                this.ActionDropDownList.Visible = false;
            } 
            string extname = "ext_"+edt.BaseProperty.Name;
            if (ThisPage.Request.Params["extname"] != null)
            {
                FieldValueControl = ThisPage.Request.Params["extname"].ToString();
            }

        }
        /// <summary>
        /// 过滤条件的值
        /// </summary>
        public string FieldValueControl
        {
            get
            {
                if (fieldContentTextBox.Visible)
                {
                    return fieldContentTextBox.Text;
                }
                if (fieldDropDownList.Visible)
                {
                    return fieldDropDownList.SelectedValue;
                }
                if (EnumRadioButtonList.Visible)
                {
                    return EnumRadioButtonList.SelectedValue;
                }
                return "";
            }
            set
            {
                if (!ThisPage.IsPostBack)
                {
                    if (fieldContentTextBox.Visible)
                    {
                        fieldContentTextBox.Text = value;
                    }
                    if (fieldDropDownList.Visible)
                    {
                        fieldDropDownList.SelectedValue = value;
                    }
                    if (EnumRadioButtonList.Visible)
                    {
                        EnumRadioButtonList.SelectedValue = value;
                    }
                }
            }
        }

        /// <summary>
        /// 过滤条件的运算符号
        /// </summary>
        public string ActionValueControl
        {
            get
            {
                if (ActionDropDownList.Visible)
                {
                    return ActionDropDownList.SelectedValue;
                }
                return "";
            }
            set
            {
                if (ActionDropDownList.Visible && !ThisPage.IsPostBack)
                {
                    ActionDropDownList.Text = value;
                }
            }
        }



        /// <summary>
        /// sql过滤条件
        /// </summary>
        /// <returns></returns>
        public string SqlFieldString()
        {
            if (EnumRadioButtonList == null)
            {
                CreateChildControls();
            }
            string tmpstr = "";
            if (EnumRadioButtonList.Visible)
            {
                if (EnumRadioButtonList.SelectedValue == "-1")
                {
                    tmpstr = " and 1=1 ";
                }
                else
                {
                    tmpstr = " and " + this.FieldName + " = " + this.EnumRadioButtonList.SelectedValue;
                }
            }
            else
            {
                if (ActionDropDownList.Visible)
                {
                    tmpstr = " and " + this.FieldName + " " + this.ActionDropDownList.SelectedValue + " '" + this.FieldValueControl + "' ";
                }
                else
                {
                    tmpstr = " and " + this.FieldName + " like '%" + this.FieldValueControl + "%' ";
                }
            }
            return tmpstr;
        }


        Page _thisPage;
        public Page ThisPage
        {
            get
            {
                if (Page == null)
                {
                    return _thisPage;
                }
                else
                {
                    return Page;
                }
            }
            set
            {
                _thisPage = value;
            }
        }
        #endregion


    }
}
